﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PH.Model.Services;
namespace PH.Data.Services
{
    public class SignatureCommand
    {
        public static void CreateNew(SignatureModel model)
        {
            using (DBCentre db = new DBCentre())
            {
                var d = Convertor.ConvertFrom<Signature, SignatureModel>(model);
                d.CreatedOn = DateTime.Now;
                db.DataContext.Signatures.Add(d);
                db.DataContext.SaveChanges();
            }
        }

        public static SignatureModel GetModel(string signature)
        {
            if (!string.IsNullOrEmpty(signature))
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Signatures.FirstOrDefault(p => p.Token== signature);
                    if (d != null)
                        return Convertor.ConvertTo<SignatureModel, Signature>(d);
                }
            }
            return null; 
        }

        public static bool Exist(string signature)
        {
            if (!string.IsNullOrEmpty(signature))
            {
                using (DBCentre db = new DBCentre())
                {
                    return db.DataContext.Signatures.Any(p => p.Token == signature);
                }
            }
            return false;
        }
    }
}
